Module WinApi

    '''CONST
    Public Const WM_GETMINMAXINFO As Integer = &H24
    Public Const WM_VSCROLL As Integer = &H115
    Public Const WM_COMMAND As Integer = &H111
    Public Const WM_NOTIFY As Integer = &H4E
    Public Const WM_KEYDOWN As Integer = &H100
    Public Const WM_MOVE As Integer = &H3
    Public Const WM_MOUSEHOVER As Integer = &H2A1
    Public Const WM_KEYUP As Integer = &H101
    Public Const WM_SYSKEYDOWN As Integer = &H10
    Public Const WM_MOUSEMOVE As Integer = &H200
    Public Const WM_CONTEXTMENU As Integer = &H7B
    Public Const WM_LBUTTONDOWN As Integer = &H201

    Public Const VK_TAB As Integer = &H9

    Public Const CW_USEDEFAULT As Integer = &H80000000

    Public Const SW_NORMAL As Integer = 1
    Public Const SW_SHOW As Integer = 5

    Public Const WS_EX_STATICEDGE As Integer = &H20000
    Public Const WS_EX_TRANSPARENT As Integer = &H20&
    Public Const WS_CHILD As Integer = &H40000000
    Public Const WS_BORDER As Integer = &H800000
    Public Const WS_VISIBLE As Integer = &H10000000
    Public Const WS_TABSTOP As Integer = &H10000
    Public Const WS_HSCROLL As Integer = &H100000
    Public Const WS_VSCROLL As Integer = &H200000
    Public Const WS_OVERLAPPEDWINDOW As Integer = (WS_OVERLAPPED Or _
                                                   WS_CAPTION Or _
                                                   WS_SYSMENU Or _
                                                   WS_THICKFRAME _
                                                   Or WS_MINIMIZEBOX _
                                                   Or WS_MAXIMIZEBOX)
    Public Const WS_OVERLAPPED As Integer = &H0
    Public Const WS_CAPTION As Integer = &HC00000
    Public Const WS_SYSMENU As Integer = &H80000
    Public Const WS_THICKFRAME As Integer = &H40000
    Public Const WS_MINIMIZEBOX As Integer = &H20000
    Public Const WS_MAXIMIZEBOX As Integer = &H10000
    Public Const WS_CLIPCHILDREN As Integer = &H2000000

    Public Const HWND_TOPMOST As Integer = -1
    Public Const HWND_NOTOPMOST As Integer = -2
    Public Const SWP_NOSIZE As Integer = &H1
    Public Const SWP_NOMOVE As Integer = &H2
    Public Const SWP_NOACTIVATE As Integer = &H10
    Public Const SWP_SHOWWINDOW As Integer = &H40
    Public Const SWP_NOREDRAW As Integer = &H8
    Public Const HWND_TOP As Integer = 0

    Public Const GW_CHILD As Integer = 5
    Public Const GW_HWNDNEXT As Integer = 2

    Public Const WM_GETTEXT As Integer = &HD
    Public Const WM_GETTEXTLENGTH As Integer = &HE

    Public Const BM_SETSTATE As Integer = &HF3
    Public Const WM_LBUTTONUP As Integer = &H202


    '''STRUCTURE
    Public Structure POINTAPI
        Dim x As Integer
        Dim y As Integer
    End Structure

    Public Structure MINMAXINFO
        Dim ptReserved As POINTAPI   ''It's good also with Point
        Dim ptMaxSize As POINTAPI
        Dim ptMaxPosition As POINTAPI
        Dim ptMinTrackSize As POINTAPI
        Dim ptMaxTrackSize As POINTAPI
    End Structure


    Public Structure CREATESTRUCT
        Dim lpCreateParams As Int32
        Dim hInstance As Int32
        Dim hMenu As Int32
        Dim hWndParent As Int32
        Dim cy As Int32
        Dim cx As Int32
        Dim y As Int32
        Dim x As Int32
        Dim style As Int32
        Dim lpszName As String
        Dim lpszClass As String
        Dim ExStyle As Int32
    End Structure



    '''FUNCTION
    Public Declare Function MoveWindow Lib "user32" _
            (ByVal hwnd As Int32, _
            ByVal x As Int32, _
            ByVal y As Int32, _
            ByVal nWidth As Int32, _
            ByVal nHeight As Int32, _
            ByVal bRepaint As Boolean) As Int32

    Public Declare Sub SetWindowPos Lib "User32" _
            (ByVal hWnd As Int32, _
                ByVal hWndInsertAfter As Int32, _
                ByVal X As Int32, _
                ByVal Y As Int32, _
                ByVal cx As Int32, _
                ByVal cy As Int32, _
                ByVal wFlags As Int32)


    Public Declare Function FreeLibrary Lib "kernel32" (ByVal hLibModule As Int32) As Int32
    Public Declare Function LoadLibrary Lib "kernel32" Alias "LoadLibraryA" (ByVal lpLibFileName As String) As Int32

    Public Declare Function SetFocus Lib "user32" Alias "SetFocus" (ByVal hwnd As Int32) As Int32

    Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
            (ByVal hwnd As Int32, _
            ByVal wMsg As Int32, _
            ByVal wParam As Int32, _
            ByVal lParam As Int32) As Int32

    Public Declare Function SendMessageSTR Lib "user32.dll" _
        (ByVal hwnd As IntPtr, _
         ByVal wMsg As Integer, _
         ByVal wparam As Integer, _
         ByVal lparam As System.Text.StringBuilder) As IntPtr


    'Public Declare Function CreateWindowEx Lib "user32" Alias "CreateWindowExA" _
    '        (ByVal dwExStyle As Int32, _
    '        ByVal lpClassName As String, _
    '        ByVal lpWindowName As String, _
    '        ByVal dwStyle As Int32, _
    '        ByVal x As Int32, _
    '        ByVal y As Int32, _
    '        ByVal nWidth As Int32, _
    '        ByVal nHeight As Int32, _
    '        ByVal hWndParent As IntPtr, _
    '        ByVal hMenu As Int32, _
    '        ByVal hInstance As Int32, _
    '        ByVal lpParam As CREATESTRUCT) As Int32

    Public Declare Function CreateWindowEx Lib "user32" Alias "CreateWindowExA" _
        (ByVal dwExStyle As Integer, _
         ByVal lpClassName As String, _
         ByVal lpWindowName As String, _
         ByVal dwStyle As Integer, _
         ByVal x As Integer, _
         ByVal y As Integer, _
         ByVal nWidth As Integer, _
         ByVal nHeight As Integer, _
         ByVal hWndParent As IntPtr, _
         ByVal hMenu As IntPtr, _
         ByVal hInstance As IntPtr, _
         ByVal lpParam As IntPtr) As Int32 'IntPtr

    Public Declare Function ShowWindow Lib "user32" (ByVal hwnd As Int32, ByVal nCmdShow As Int32) As Int32
    Public Declare Function DestroyWindow Lib "user32" (ByVal hwnd As Int32) As Int32

    Public Declare Function SetEnvironmentVariable Lib "kernel32" Alias "SetEnvironmentVariableA" _
            (ByVal lpName As String, ByVal lpValue As String) As Int32


    Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
            (ByVal lpClassName As String, _
             ByVal lpWindowName As String) As Int32

    Public Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" _
            (ByVal hWnd1 As Int32, _
             ByVal hWnd2 As Int32, _
             ByVal lpsz1 As String, _
             ByVal lpsz2 As String) As Int32

    Public Declare Function GetWindow Lib "user32" _
            (ByVal hwnd As Int32, _
             ByVal wCmd As Int32) As Int32

    Public Declare Function GetClassName Lib "user32" Alias "GetClassNameA" _
            (ByVal hwnd As Int32, _
             ByVal lpClassName As String, _
             ByVal nMaxCount As Int32) As Int32

    'Public Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" _
    '        (ByVal hwnd As Int32, _
    '         ByVal lpString As String, _
    '         ByVal cch As Int32) As Int32




    Public Declare Function SetTextColor Lib "gdi32" _
            (ByVal HDC As IntPtr, _
            ByVal crColor As Int32) As Int32


    Public Declare Function VarPtrAny Lib "vb40032.dll" Alias "VarPtr" (ByVal lpObject As Object) As Int32


    Public Declare Function GetDC Lib "user32" (ByVal hwnd As Int32) As IntPtr

    Public Declare Function GetTickCount Lib "kernel32" Alias "GetTickCount" () As Int32


    '''OTHER FUNCTIONS
    Public Function LoWord(ByVal DWord As Int32) As Integer
        If DWord And &H8000& Then ' &H8000& = &H00008000
            Return DWord Or &HFFFF0000
        Else
            Return DWord And &HFFFF&
        End If
    End Function

    Public Function HiWord(ByVal DWord As Int32) As Integer
        Return (DWord And &HFFFF0000) \ &H10000
    End Function

    Public Function MakeDWord(ByVal LoWord As Integer, ByVal HiWord As Integer) As Long
        Return (HiWord * &H10000) Or (LoWord And &HFFFF&)
    End Function

    Public Function LoByte(ByVal wParam As Integer) As Long
        Return wParam And &HFF&
    End Function

    Public Function HiByte(ByVal wParam As Integer) As Long
        Return wParam \ &H100 And &HFF&
    End Function

    Public Function VarPtr(ByVal o As Object) As Integer

        Dim GC As System.Runtime.InteropServices.GCHandle = _
                System.Runtime.InteropServices.GCHandle.Alloc(o, _
                        System.Runtime.InteropServices.GCHandleType.Pinned)

        Dim ret As Integer = GC.AddrOfPinnedObject.ToInt32

        GC.Free()

        Return ret

    End Function

    Public Function VarPtrIntPtr(ByVal o As Object) As IntPtr

        Dim GC As System.Runtime.InteropServices.GCHandle = _
                System.Runtime.InteropServices.GCHandle.Alloc(o, _
                        System.Runtime.InteropServices.GCHandleType.Pinned)

        Dim ret As IntPtr = GC.AddrOfPinnedObject

        GC.Free()

        Return ret

    End Function

    Public Function GetWindows(ByVal ParentWindowHandle As Int32) As IntPtr()

        Dim ptrChild As IntPtr
        Dim ptrRet() As IntPtr = Nothing
        Dim iCounter As Integer

        'get first child handle... 
        ptrChild = GetWindow(ParentWindowHandle, GW_CHILD)

        'loop through and collect all child window handles... 
        Do Until ptrChild.Equals(IntPtr.Zero)
            'process child... 
            ReDim Preserve ptrRet(iCounter)
            ptrRet(iCounter) = ptrChild
            'get next child... 
            ptrChild = GetWindow(ptrChild, GW_HWNDNEXT)
            iCounter += 1
        Loop

        'return... 
        Return ptrRet

    End Function

    Public Function GetWindowText(ByVal WindowHandle As IntPtr) As String

        Dim ptrRet As IntPtr
        Dim ptrLength As IntPtr

        'get length for buffer... 
        ptrLength = SendMessage(WindowHandle, WM_GETTEXTLENGTH, IntPtr.Zero, IntPtr.Zero)

        'create buffer for return value... 
        Dim sbText As New System.Text.StringBuilder(ptrLength.ToInt32 + 1)

        'get window text... 
        ptrRet = SendMessageSTR(WindowHandle, WM_GETTEXT, ptrLength.ToInt32 + 1, sbText)

        'get return value... 
        Return sbText.ToString

    End Function


    Public Sub ClickButton(ByVal ButtonHandle As IntPtr)

        'send the left mouse button "down" message to the button... 
        Call SendMessage(ButtonHandle, WM_LBUTTONDOWN, 0, IntPtr.Zero)

        'send the left mouse button "up" message to the button... 
        Call SendMessage(ButtonHandle, WM_LBUTTONUP, 0, IntPtr.Zero)

        'send the button state message to the button, telling it to handle its events... 
        Call SendMessage(ButtonHandle, BM_SETSTATE, 1, IntPtr.Zero)

    End Sub

End Module
